<template>
  <div>
    <el-dialog
      v-model="state.dialogVisible"
      title="上传合同扫描件"
      width="50%"
      align-center
      @open="openHandler"
      @close="handleClose"
      class="dialogStyle"
    >
      <el-form
        :model="formInline"
        :rules="rules"
        ref="formRef"
        class="demo-form-inline"
        label-width="140px"
      >
        <el-form-item label="招标项目任务" class="oneline">
          <el-input
            disabled
            v-model="formInline.projectTaskName"
            placeholder=""
          />
        </el-form-item>
        <el-form-item label="合同签订时间" class="oneline" prop="signTime">
          <el-date-picker
            :disabled="state.type"
            v-model="formInline.signTime"
            type="date"
            value-format="YYYY-MM-DD"
            placeholder="选择实际完成时间"
          />
        </el-form-item>
        <el-form-item label="上传合同附件" class="oneline" prop="file">
          <FileMinioUpload
            :disabled="state.type"
            :businessType="21"
            :associationId="formInline.projectTaskId"
            :restriction="true"
            :restrictionLimit="1"
            v-model:files="formInline.file"
            :show-button="!state.type"
          ></FileMinioUpload>
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="state.dialogVisible = false" class="button_cancel2">取消</el-button>
          <el-button @click="submitHandler" v-if="!state.type" class="button_view">
            确定
          </el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>
<script setup>
import { ref, reactive } from 'vue'
import { ElDialog, ElForm, ElFormItem, ElMessage } from 'element-plus'
import FileMinioUpload from "@/components/FileMinioUpload.vue";
import { contractResult } from '@/api/project'

let dialogVisible = ref(false)
const formInline = reactive({
  file: [],
  signTime: ''
})
const state = reactive({
  projectTaskId: '',
  type: 0,
  businessId: '',
  dialogVisible: false,
  projectRow: {}
})
const rules = {
  file: [{ required: true, message: '文件不能为空', trigger: 'blur' }],
  signTime: [{ required: true, message: '签订时间不能为空', trigger: 'blur' }]
}
//弹框打开前的回调
const openHandler = async () => {
  let obj = {
    projectTaskId: state.projectTaskId || '',
    businessId: state.businessId || '',
    itemId: state.itemId || '',
  }
  await contractResult.getInfo(obj).then((res) => {
    if (res.code == '200') {
      let obj = { ...res.data, ...res.data?.taskInfo }
      for (const key in obj) {
        if (key == 'file') {
          if (obj[key] instanceof Object&&obj[key].fileId) {
            formInline[key] = []
            formInline[key].push(obj[key])
          } else {
            formInline[key] = []
          }
          continue
        }
        console.log(formInline[key],'sss');
        formInline[key] = obj[key] || ''
      }
    }
  })
}
const emits = defineEmits(['refreshList'])
//弹框关闭
const handleClose = () => {
  for (const key in formInline) {
    formInline[key] = ''
  }
  formInline.file = []
  emits('refreshList')
}

const formRef = ref(null)
//附件提交
const submitHandler = () => {
  formRef.value.validate(async (valid, fields) => {
    if (valid) {
      await contractResult
        .save({
          itemId: state.itemId,
          file: formInline.file[0],
          projectTaskId: state.projectTaskId,
          signTime: formInline.signTime + ' 00:00:00'
        })
        .then((res) => {
          if (res.code == '200') {
            state.dialogVisible = false
            ElMessage.success('操作成功')
          }
        })
    }
  })
}
defineExpose({
  state
})
</script>
<style lang="scss" scoped>
@import '@/views/constructionProjectManage/static/dialog.scss';
</style>
